//need the two following lines of code or else whole thing will break
varying highp vec2 textureCoordinate;
//uniform float intensity111;
uniform sampler2D inputImageTexture;
/*
Notes:
 
lowp = low precison, mediump = medium precision, highp = high precision
lowp = faster preformence
highp = slower preformance but higher precision
 */


//The Daltonize algorithm implemented as a fragment shader
void main()
{
   
//grab the rgb values here
lowp vec3 pixcol = texture2D(inputImageTexture, textureCoordinate).rgb;

//RGB to LMS transform:  important in mat3 every three successive group of numbers represents a coloum NOT ROW.
lowp vec3 RGB_Colors = vec3(pixcol.r, pixcol.g, pixcol.b);
mediump mat3 RGB2LMS = mat3(17.8824, 3.45565, .0299566, 43.5262, 27.1554, .0184309, 4.1193, 3.86714, 1.46709);
lowp vec3 LMS_RGB_Values = RGB2LMS * RGB_Colors;

//mapping of normal cone responses LMS to protanope cone responses
mediump mat3 LMS2ProtaopeConeResponseMatrix = mat3(0.0, 0.0, 0.0, 2.02344, 1.0, 0.0, -2.52581, 0.0, 1.0);
lowp vec3 LP_MP_SP = LMS2ProtaopeConeResponseMatrix*LMS_RGB_Values;

//now calcuate colour precieved by protanope
mediump mat3 RGBProtanopeColorPerception = mat3(0.0809444479, -0.0102485335, -0.000365296938, -0.130504409, 0.0540193266, -0.00412161469, 0.116721066, -0.113614708, 0.693511405);
lowp vec3 RP_GP_BP = RGBProtanopeColorPerception*LP_MP_SP;

//--------------------up to here is what a color blind persons would see-----------------------

//calculate the error
mediump vec3 errorVector = vec3(abs(pixcol.r-RP_GP_BP[0]), abs(pixcol.g-RP_GP_BP[1]), abs(pixcol.b-RP_GP_BP[2]));

//matrix for shifting everything to the blue spectrum
mediump mat3 M = mat3(0.0, 0.7, 0.7, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0);

//produce the error modification representing 
lowp vec3 errMod = M*errorVector;

//add back the error modification to the original pixels to complete the daltonization
lowp vec3 daltonizedVector = vec3((pixcol.r + errMod[0]), (pixcol.g + errMod[1]), (pixcol.b + errMod[2]));

//send daltonized pixels + aplha to the fragment shader
gl_FragColor = vec4(daltonizedVector,1.0);//LMS_RGB_Values, 1.0);
    
}
